package org.credo.thread.tongbu;

import java.util.concurrent.locks.ReentrantLock;

public class Account {
	private String account;
	private double money;
	private final ReentrantLock lock=new ReentrantLock();
	
	public Account(String account,double money){
		this.account=account;
		this.money=money;
	}
	
	public void process(double hopeGetMoney){
		try {
			lock.lock();
		if(money >= hopeGetMoney){
			System.out.println(Thread.currentThread().getName()+"取钱成功,取出金额为:"+hopeGetMoney);
			try {
				Thread.sleep(1);
			} catch (Exception e) {
				e.printStackTrace();
			}
			//修改金额
			this.money=money-hopeGetMoney;
			System.out.println("余额为:"+money);
		}else{
			System.out.println("余额不足,取款失败!");
		}
		} finally {
			lock.unlock();
		}
	}

	
	//根据account重写hashcode和equals方法.
	public int hasCode(){
		return account.hashCode();
	}
	public boolean equals(Object obj){
		if(this==obj){
			return true;
		}
		if(obj!=null && obj.getClass()==Account.class){
			Account target=(Account)obj;
			return target.getAccount().equals(account);
		}
		return false;
	}
	
	public String getAccount() {
		return account;
	}
	public void setAccount(String account) {
		this.account = account;
	}
	public double getMoney() {
		return money;
	}
	public void setMoney(double money) {
		this.money = money;
	}
}
